package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DJobNewsMapper;
import com.weipai.demo.mapper.base.JobCategoryMapper;
import com.weipai.demo.mapper.base.JobNewsMapper;
import com.weipai.demo.model.JobCategory;
import com.weipai.demo.model.JobNews;
import com.weipai.demo.service.NewsShowService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/14 11:34
 * @Version 1.0
 */
@Service
public class NewsShowServicelmpl implements NewsShowService {

    private static final Logger log = LoggerFactory.getLogger(NewsShowServicelmpl.class);

    @Autowired
    private JobCategoryMapper jobCategoryMapper;
    @Autowired
    private JobNewsMapper jobNewsMapper;
    @Autowired
    private DJobNewsMapper dJobNewsMapper;


    //分页显示消息列表
    @Override
    public LayuiTablePagingResult NewsShow(Integer page, Integer size, JobNews jobNews) {
        LayuiTablePagingResult result= null;
        try {
            PageHelper.startPage(page,size);

            Example example = new Example(JobNews.class);
            example.setOrderByClause("sort ASC");
            example.createCriteria().andEqualTo("jobCategoryId", jobNews.getJobCategoryId());
            List<JobNews> banners = jobNewsMapper.selectByExample(example);
            //List<JobNews> banners = jobNewsMapper.select(jobNews);

            PageInfo<JobNews> pageInfo=new PageInfo<>(banners);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return result;

    }


    //显示消息类别列表
    @Override
    public Response NewsShowList(JobCategory jobCategory) {
        Response response=new Response();
        if (jobCategory.getVisible()!=null){
            List<JobCategory> jobCategories = jobCategoryMapper.selectAll();
            response.put("jobCategories",jobCategories);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //后台按照标题关键字查询
    @Override
    public LayuiTablePagingResult searchTitle(Integer page,Integer size,JobNews jobNews) {

        LayuiTablePagingResult result = null;
        try {
            PageHelper.startPage(page,size);
            List<JobNews> newsList = jobNewsMapper.select(jobNews);

            PageInfo<JobNews> pageInfo=new PageInfo<>(newsList);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return result;
    }

    //保存图片
    @Override
    public Response savePicture(JobNews jobNews) {
        Response response = new Response();
        jobNewsMapper.insert(jobNews);
        response.setCode(RetCode.SUCCESS);

        return response;
    }


}
